<template>
  <div>
    <div>
      <el-button type="primary" @click="openAdd">添加</el-button>
    </div>
    <div>
      <el-table :data="tableData" style="width: 100%">
        <el-table-column label="模版ID" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.id }}</span>
          </template>
        </el-table-column>
        <el-table-column label="分类模板名称" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="关联品牌" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              scope.row.brandIds | filterStrs
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="关联规格" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              scope.row.specIds | filterStrs
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="扩展属性" width="180">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{
              scope.row.customAttributeItems | filterStrs
            }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.$index, scope.row)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div>
      <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
        <el-form :model="form">
          <el-form-item label="商品类型" :label-width="formLabelWidth">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
          {{ brandListValue }}
          <el-form-item label="关联品牌" :label-width="formLabelWidth">
            <el-select v-model="brandListValue" multiple placeholder="请选择">
              <el-option
                v-for="item in brandList"
                :key="item.id"
                :label="item.name"
                :value="JSON.stringify({ id: item.id, text: item.name })"
              >
              </el-option>
            </el-select>
            <!-- [{"id":27,"text":"网络"}] -->
          </el-form-item>
          {{ specIdsListValue }}
          <el-form-item label="关联规格" :label-width="formLabelWidth">
            <el-select v-model="specIdsListValue" multiple placeholder="请选择">
              <el-option
                v-for="item in specIdsList"
                :key="item.id"
                :label="item.specName"
                :value="JSON.stringify({ id: item.id, text: item.specName })"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="扩展属性" :label-width="formLabelWidth">
            <el-button @click="appendAttr">新增扩展属性</el-button>
            {{ customAttributeItemsList }}
            <el-table
              ref="multipleTable"
              :data="customAttributeItemsList"
              tooltip-effect="dark"
              style="width: 100%"
              @selection-change="handleSelectionChange"
            >
              <!-- <el-table-column type="selection" width="55"> </el-table-column> -->
              <el-table-column label="属性名称" width="120">
                <template slot-scope="scope">
                  <el-input
                    v-model="scope.row.text"
                    placeholder="请输入内容"
                  ></el-input>
                </template>
              </el-table-column>
              <el-table-column prop="name" label="操作" width="120">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="danger"
                    @click="delAttr(scope.$index)"
                    >删除</el-button
                  >
                </template>
              </el-table-column>
            </el-table>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { brandList } from "@/api/brandApi";
import { listApi } from "@/api/typeTemplate";
import { listAll } from "@/api/brandApi";
import { specListAll } from "@/api/spcList";
import { save, findById } from "@/api/typeTemplate";
export default {
  data() {
    return {
      tableData: [],
      dialogFormVisible: false,
      form: {
        id: "",
        name: "",
        specIds: "",
        brandIds: "",
        customAttributeItems: "",
      },
      brandListValue: [],
      customAttributeItemsList: [],
      brandList: [],
      specIdsList: [],
      specIdsListValue: [],
    };
  },
  created() {
    this.list(
      listApi({}).then((result) => {
        this.tableData = result.data.data.list;
        console.log(this.tableData);
      })
    );
  },
  methods: {
    saveOrUpdate() {
      this.form.customAttributeItems = JSON.stringify(
        this.customAttributeItemsList
      );
      this.form.specIds = this.listToStr(this.specIdsListValue);
      this.form.brandIds = this.listToStr(this.brandListValue);
      save(this.form).then((result) => {
        this.dialogFormVisible = false;
        this.list();
      });
    },
    listToStr(list) {
      var newList = [];
      console.log("list,list,list");
      console.log(list);
      for (var i = 0; i < list.length; i++) {
        newList.push(JSON.parse(list[i]));
      }
      console.log("newList,newList,newList");
      console.log(newList);
      return JSON.stringify(newList);
    },
    strToList(strList) {
      var newList = [];
      var list = JSON.parse(strList);
      for (var i = 0; i < list.length; i++) {
        newList.push(JSON.stringify(list[i]));
      }
      console.log("==================");
      console.log(newList);
      return newList;
    },
    appendAttr() {
      this.customAttributeItemsList.push({});
    },
    delAttr(index) {
      this.customAttributeItemsList.splice(index, 1);
    },
    openAdd() {
      listAll().then((result) => {
        this.brandList = result.data.data;
      });
      specListAll().then((result) => {
        this.specIdsList = result.data.data;
      });
      this.dialogFormVisible = true;
    },
    list() {},
    handleEdit(index, row) {
      this.form = [];
      findById(row.id).then((result) => {
        this.form = result.data.data;
        this.customAttributeItemsList = JSON.parse(
          this.form.customAttributeItems
        );
        listAll().then((result) => {
          this.brandList = result.data.data;
          this.brandListValue = this.strToList(this.form.brandIds);
        });
        specListAll().then((result) => {
          this.specIdsList = result.data.data;
          this.specIdsListValue = this.strToList(this.form.specIds);
        });
        this.dialogFormVisible = true;
      });
    },
    handleDelete(index, row) {
      console.log(index, row);
    },
  },
  filters: {
    filterStrs(strList) {
      if (strList == null || strList == "") {
        return;
      }
      var list = JSON.parse(strList);
      var str = [];
      for (var i = 0; i < list.length; i++) {
        str += list[i].text + ",";
      }
      return str.substring(0, str.length - 1);
    },
  },

  // filters: {
  //   filterStr(strList) {
  //     if (strList == null || strList == "") {
  //       return;
  //     }
  //     var list = JSON.parse(strList);
  //     var str = "";
  //     for (var i = 0; i < list.length; i++) {
  //       str += list[i].text + ",";
  //     }
  //     return str.substring(0, str.length - 1);
  //   },
  // },
};
</script>

<style scoped></style>
